import Vue from 'vue'
import Vuex from 'vuex'


Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    //loading的配置
    loadingConfig:{
      isShowLoading:false,
      title:"正在加载"
    },
    //底部菜单的配置
    actionSheetConfig:{
      //是否显示actionSheet
      isShowActionSheet:false,
      //菜单选项
      itemList:[],
      //这个回掉函数指的就是当你点击某一项以后进行的操作
      callBack:null
    },
    //预览图片的配置
    previewImageConfig:{
      isShowPreviewImage:false,
      urls:[],//所有图片预览的集合
      cerrent:""
    }
  },
  mutations: {
    changeLoadingConfig(state,arg){
      state.loadingConfig=arg;
    },
    showActionSheet(state,config){
      
      if(config&&config.itemList&&config.itemList.length>0){
        state.actionSheetConfig.itemList=config.itemList;
        state.actionSheetConfig.isShowActionSheet=true;
      }
      else{
        throw new Error("不能为空")
      }
      if(config&&config.success&&typeof config.success==="function"){
        state.actionSheetConfig.callBack=config.success;
        //把回调方法赋值进去
      }
      
    },
    hideActionSheet(state){
      state.actionSheetConfig.isShowActionSheet=false;
    },
    //================== 预览图片===============
    showPreviewImage(state,config){
      state.previewImageConfig.isShowPreviewImage=true;
      if(config&&config.urls&&config.urls.length>0){
        state.previewImageConfig.urls=config.urls;
      }
      if(config&&config.current){
        state.previewImageConfig.current=config.current
      }
    },
    hidepreviewImage(state){
      state.previewImageConfig.isShowPreviewImage=false;

    }
  },
  actions: {
    //显示
    showLoading({commit},title){
      if(title&&title.length>5){
        throw new Error("loading的文字最多只能有5个");
       commit("changeLoadingConfig",{isShowLoading:true,title:""});
      }
      commit("changeLoadingConfig",{isShowLoading:true,title:title})
    },
    //隐藏
    hideLoading({commit}){
      commit("changeLoadingConfig",{isShowLoading:false,title:"正在加载"})

    },
    //显示
    showActionSheet({commit},config){
      commit("showActionSheet",config)
    },
    hideActionSheet({commit}){
      console.log("show")
      commit("hideActionSheet")
    },
    showPreviewImage({commit},config){
      commit("showPreviewImage",config)
    },
    hidePreviewImage({commit}){
      commit("hidepreviewImage")
    }
  },
  //方便获取值
  getters:{
    //是否显示加载
    isShowLoading(state){
      return state.loadingConfig.isShowLoading;
    },
    //加载当中的文字
    loadingText(state){
      return state.loadingConfig.title;
    },
    isShowActionSheet(state){
      return state.actionSheetConfig.isShowActionSheet;
    },
    actionSheetItemList(state){
      return state.actionSheetConfig.itemList;
    },
    actionSheetCallBack(state){
      return state.actionSheetConfig.callBack;
    },
    //===========预览图片============
    isShowPreviewImage(state){
      return state.previewImageConfig.isShowPreviewImage
    },
    previewImageUrls(state){
      return state.previewImageConfig.urls;
    },
    currentPriviewImage(state){
      return state.previewImageConfig.current;
    }
  }
})
